﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace NetScript.Core
{
    //Creator - HF Tweety
    //Profile - http://www.hackforums.net/member.php?action=profile&uid=1234585
    //Website - www.codeineject.co.uk
    //Twitter - @Codeinject
    /// <summary>
    /// Net Script is a light weight application to run text files as .NET application or scripts
    /// It transforms the language into a scripting language, making it as powerful and easy to use as Batch files or VB 
    /// </summary>
    /// <remarks>
    /// </remarks>
    static class Virtualize
    {
        public const string  VBC = ".vbc";
        public const string CSC = ".csc";
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
             InitializeMutex("NetScript.Core");
            if (Environment.GetCommandLineArgs().Length == 0) {Environment.Exit(0);}

            string[] arguments = Environment.GetCommandLineArgs();
            
                foreach (string path in arguments)
                    try
                        {
                            {
                                if (path.Contains(System.Windows.Forms.Application.ExecutablePath)) { continue; }
                                if (Path.GetExtension(path) == VBC)
                                    Process.Start(Application.StartupPath + "\\vbc.exe", '"' + path + '"');
                                if (Path.GetExtension(path) == CSC)
                                    Process.Start(Application.StartupPath + "\\csc.exe", '"' + path + '"');
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.InnerException.ToString());
                System.Windows.Forms.MessageBox.Show(ex.Message,
                                                        "Error",
                      System.Windows.Forms.MessageBoxButtons.OK,
                      System.Windows.Forms.MessageBoxIcon.Error);
                System.Windows.Forms.MessageBox.Show(ex.InnerException.ToString(),
                                                        "Error",
                      System.Windows.Forms.MessageBoxButtons.OK,
                      System.Windows.Forms.MessageBoxIcon.Error);
            }
        }
        private static void InitializeMutex(String Mutex)
        {
            lock (Mutex)
            {
                bool Sys = false;
                System.Threading.Mutex Obj = new System.Threading.Mutex(Sys, Mutex);
                if (Sys) { Environment.Exit(0); }
            }
        }
    }
}
